<template>
  <el-breadcrumb
    separator="/"
    class="mb-1"
    :class="{ 'mx-1': useLocale.isMob }"
    style="vertical-align: bottom"
  >
    <el-breadcrumb-item>
      <div class="flex align-center">
        <el-icon :size="18"> <Goods /> </el-icon
        ><span class="pl">电商管理</span>
      </div>
    </el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  </el-breadcrumb>
  <div v-if="useLocale.isMob">
    <el-alert
      style="padding: 3px 15px"
      title="當你使用文件批量添加商品時，'分類ID' 請到 電商管理->商品分類 中查找。"
      type="warning"
      :closable="false"
    />
    <div class="mx-15 my-15 flex justify-between">
      <el-space :size="10" wrap>
        <el-button type="primary" size="small" plain round>新增</el-button>
        <el-dropdown>
          <el-button type="primary" size="small" round>
            到货管理<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>取消到货</el-dropdown-item>
              <el-dropdown-item>商品到貨</el-dropdown-item>
              <el-dropdown-item>斷貨</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-dropdown type="primary">
          <el-button type="primary" size="small" round>
            導入商品<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>導入商品</el-dropdown-item>
              <el-dropdown-item>導入商品模版下載</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-dropdown type="primary">
          <el-button type="primary" size="small" round>
            下載<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>下載</el-dropdown-item>
              <el-dropdown-item>下載(樣式)</el-dropdown-item>
              <el-dropdown-item>下載(選取)</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-dropdown type="primary">
          <el-button type="primary" size="small" round>
            批量操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>上架</el-dropdown-item>
              <el-dropdown-item>下架</el-dropdown-item>
              <el-dropdown-item>刪除</el-dropdown-item>
              <el-dropdown-item>隱藏</el-dropdown-item>
              <el-dropdown-item>顯示</el-dropdown-item>
              <el-dropdown-item>修改分類</el-dropdown-item>
              <el-dropdown-item>更新有效期</el-dropdown-item>
              <el-dropdown-item>全部刪除</el-dropdown-item>
              <el-dropdown-item>全部隱藏</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-space>
      <el-button
        :icon="Search"
        size="small"
        circle
        @click="
          () => {
            rel.mobFilter = true;
          }
        "
      />
    </div>

    <div class="mob-list mt-2">
      <el-scrollbar ref="scrollbarRef" class="mob-scroll">
        <div
          class="mob-list-item text-content-color font"
          v-for="(item, index) in tableData"
        >
          <div class="flex align-center mb-1">
            <el-checkbox
              v-model="item.checked"
              @change="(value) => mobChecked(value, item)"
            />
            <el-avatar
              class="ml-1 rounded-sm"
              :size="60"
              fit="fill"
              shape="”square“"
              :src="item.goods_img"
            />
            <div class="flex-1 ml-15">
              <div class="flex justify-between">
                <span class="font-mder text-main-color">{{
                  item.goods_name
                }}</span>
                <span>{{ item.cat_name }}</span>
              </div>
              <div class="mt font-sm">{{ item.msg_user_id }}</div>
            </div>
          </div>
          <div wrap class="flex flex-wrap justify-end my-1">
            <el-button size="small" plain round>删除</el-button>
            <el-button size="small" plain round type="primary">编辑</el-button>
          </div>
          <div class="flex justify-between border-bottom py-15">
            <span>售价({{ rel.currency }})</span>
            <spa>{{ item.shop_price }}</spa>
          </div>
          <div class="flex justify-between border-bottom py-15">
            <span>定价({{ rel.currency }})</span>
            <spa>{{ item.market_price }}</spa>
          </div>
          <div class="flex justify-between py-15">
            <span>进价</span>
            <span>{{ item.purchase_price }}</span>
          </div>
          <div class="flex justify-between py-15">
            <span>库存</span>
            <span>{{ item.goods_num }}</span>
          </div>
          <div v-if="item.more">
            <div class="flex justify-between py-15">
              <span>商品ID</span>
              <span>{{ item.goods_id }}</span>
            </div>
            <div class="flex justify-between py-15">
              <span>SKU</span>
              <span>{{ item.warehouse_code }}</span>
            </div>
            <div class="flex justify-between py-15">
              <span>商品重量(g)</span>
              <span>{{ item.goods_weight }}</span>
            </div>
            <div class="flex justify-between py-15">
              <span>厂商名称</span>
              <span>{{ item.factory_name }}</span>
            </div>
          </div>
          <div class="flex align-center justify-center">
            <el-button link style="color: #909399" @click="handleMore(item)">
              {{item.moreTxt}}<el-icon class="pl">
                <component :is="item.moreIcon"></component>
              </el-icon>
            </el-button>
          </div>
        </div>
      </el-scrollbar>
    </div>

    <div v-if="rel.total > params.page_size" class="my-1 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :pager-count="5"
        :small="false"
        :disabled="false"
        :background="true"
        layout="prev, pager, next"
        :total="rel.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
  <el-card v-else class="box-card" shadow="hover">
    <template #header>
      <el-alert
        style="padding: 3px 5px"
        title="當你使用文件批量添加商品時，'分類ID' 請到 電商管理->商品分類 中查找。"
        type="warning"
        :closable="false"
      />
      <div class="flex justify-between flex-wrap">
        <el-space wrap :size="10" class="mt">
          <el-input
            v-model="params.goods_name"
            style="width: 150px"
            placeholder="商品名称"
          />
          <el-input
            v-model="params.warehouse_code"
            style="width: 150px"
            placeholder="SKU代码"
          />
          <el-input
            v-model="params.cat_name"
            style="width: 150px"
            placeholder="分类名称"
          />
          <el-select
            v-model="params.from_mode"
            placeholder="类型"
            style="width: 150px"
          >
            <el-option
              v-for="item in rel.morcart"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-button :icon="Search" plain @click="search">查詢</el-button>
        </el-space>
        <el-space wrap :size="10" class="mt">
          <el-button type="primary" plain>新增</el-button>
          <el-dropdown>
            <el-button type="primary">
              到货管理<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>取消到货</el-dropdown-item>
                <el-dropdown-item>商品到貨</el-dropdown-item>
                <el-dropdown-item>斷貨</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <el-dropdown type="primary">
            <el-button type="primary">
              導入商品<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>導入商品</el-dropdown-item>
                <el-dropdown-item>導入商品模版下載</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <el-dropdown type="primary">
            <el-button type="primary">
              下載<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>下載</el-dropdown-item>
                <el-dropdown-item>下載(樣式)</el-dropdown-item>
                <el-dropdown-item>下載(選取)</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <el-dropdown type="primary">
            <el-button type="primary">
              批量操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>上架</el-dropdown-item>
                <el-dropdown-item>下架</el-dropdown-item>
                <el-dropdown-item>刪除</el-dropdown-item>
                <el-dropdown-item>隱藏</el-dropdown-item>
                <el-dropdown-item>顯示</el-dropdown-item>
                <el-dropdown-item>修改分類</el-dropdown-item>
                <el-dropdown-item>更新有效期</el-dropdown-item>
                <el-dropdown-item>全部刪除</el-dropdown-item>
                <el-dropdown-item>全部隱藏</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-space>
      </div>
    </template>
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="goods_id" label="ID" />
      <el-table-column prop="goods_name" label="商品名稱" />
      <el-table-column label="商品圖片" align="center" width="150">
        <template #default="scope">
          <el-avatar
            class="ml-1 rounded-sm"
            :size="60"
            fit="fill"
            shape="”square“"
            :src="scope.row.goods_img"
          />
        </template>
      </el-table-column>
      <el-table-column prop="warehouse_code" label="SKU代号" min-width="100" />
      <el-table-column prop="cat_name" label="分類名稱" />
      <el-table-column prop="shop_price" label="售價" />
      <el-table-column prop="market_price" label="定價" />
      <el-table-column prop="purchase_price" label="進價" />
      <el-table-column prop="goods_num" label="庫存" />
      <el-table-column prop="goods_weight" label="商品重量(g)" width="100" />
      <el-table-column prop="factory_name" label="廠商名稱" />
      <el-table-column prop="special_ship_cost" label="特殊運費" />
      <el-table-column prop="goods_status" label="狀態" />
      <el-table-column prop="remarks" label="備註" width="200" />

      <el-table-column fixed="right" label="操作" width="100">
        <template #default="scope">
          <el-space direction="vertical">
            <el-button
              link
              size="small"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >編輯</el-button
            >
            <el-button
              link
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >刪除</el-button
            >
          </el-space>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="rel.total > params.page_size" class="mt-2 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :page-sizes="[50, 100, 150]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next"
        :total="rel.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
  <el-drawer v-model="rel.mobFilter" size="80%">
    <template #header>
      <h4>{{ $t("common.filter") }}</h4>
    </template>
    <template #default>
      <el-space direction="vertical" fill style="width: 100%">
        <div class="mb-1">
          <div class="font-sm text-tips-color mb">商品名称</div>
          <el-input v-model="params.goods_name" />
        </div>
        <div class="mb-1">
          <div class="font-sm text-tips-color mb">SKU代码</div>
          <el-input v-model="params.warehouse_code" />
        </div>
        <div class="mb-1">
          <div class="font-sm text-tips-color mb">分类名称</div>
          <el-input v-model="params.cat_name" />
        </div>
        <div class="mb-1">
          <div class="font-sm text-tips-color mb">类型</div>
          <el-select v-model="params.from_mode">
            <el-option
              v-for="item in rel.morcart"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </div>
      </el-space>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="mobClearFilter">{{ $t("common.clear") }}</el-button>
        <el-button type="primary" @click="search">{{
          $t("common.query")
        }}</el-button>
      </div>
    </template>
  </el-drawer>
</template>
<script lang="ts" setup>
import { nextTick, ref, onMounted, reactive, watch, computed } from "vue";
import { Search, Goods, ArrowDown,ArrowUp } from "@element-plus/icons-vue";
import { $morcart, $getGoods } from "@api/live";
import type { Component } from "vue";
import Uncart from "./cart/Uncart.vue";
import Allcart from "./cart/Allcart.vue";
import { dayjs } from "element-plus";
import { useLocaleStore } from "@/store/locales";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
const router = useRouter();
const { t } = useI18n();
const langkey = computed(() => useLocale.locale);
const useLocale = useLocaleStore();
const scrollbarRef = ref();
const myDate = ref([]);
let params = ref({
  goods_name: "",
  warehouse_code: "",
  from_mode: "",
  cat_name: "",
  current_page: 1,
  page_size: 50,
});
const rel = reactive({
  morcart: [],
  mobFilter: false,
  total: 0,
  currency: useLocale.dictionary.currency, //币种
});
const tableData = ref([]);
onMounted(() => {
  getFilter();
});
const getFilter = async () => {
  const morcart_res = await $morcart();
  rel.morcart = morcart_res.data.list;

  //初始化默认值
  getData();
};
const getData = async () => {
  let res = await $getGoods(params);
  if (res.code == 0) {
    tableData.value = res.data.table;
    rel.total = res.data.total;
    if (useLocale.isMob) {
      tableData.value.forEach(item=>{
        item.more=false;
         item.moreTxt="全部信息"
         item.moreIcon=ArrowDown
      })
      nextTick(() => {
        scrollbarRef.value.setScrollTop(0);
      });
    }
  }
};
const handleSizeChange = (val: number) => {
  params.value.page_size = val;
  getData();
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  params.value.current_page = val;
  getData();
  console.log(`current page: ${val}`);
};
const search = () => {
  console.log(params.value);
  getData();
  rel.mobFilter = false;
};

const batchUsersGroup = () => {
  if (currEuids.value.length == 0) {
    ElMessage({
      message: t("user.select-user"),
      type: "warning",
    });
    return false;
  }
  groupMuti.value = false;
  groupType.value = "users";
  currRow.value = {};
  groupRef.value.changeShow();
};
const batchBindUsers = async () => {
  if (currEuids.value.length !== 2) {
    ElMessage({
      message: t("user.bindTips2"),
      type: "warning",
    });
    return false;
  }
  ElMessageBox.confirm(t("user.bindTips"), t("common.tips"), {
    confirmButtonText: t("common.confirmBtn"),
    cancelButtonText: t("common.cancelBtn"),
    type: "warning",
  })
    .then(async () => {
      let res = await $bindUser({ euids: currEuids.value });
      if (res.code == 0) {
        ElMessage({
          message: t("common.success"),
          type: "success",
        });
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch(() => {});
};
const batchDownload = () => {
  window.location.href = "https://www.full2house.com";
};

const handleInfo = (index, row) => {
  console.log(row);
  router.push({
    path: "/user/info",
    query: {
      id: row.euid,
    },
  });
};
const quitGroup = (index, row) => {
  ElMessageBox.confirm(t("user.quitGroup"), t("common.tips"), {
    confirmButtonText: t("common.confirmBtn"),
    cancelButtonText: t("common.cancelBtn"),
    type: "warning",
    draggable: true,
  }).then(() => {
    quitUserGroup(row);
  });
};
const unbind = (index, row) => {
  ElMessageBox.confirm(t("user.unbindTips"), t("common.tips"), {
    confirmButtonText: t("common.confirmBtn"),
    cancelButtonText: t("common.cancelBtn"),
    type: "warning",
    draggable: true,
  }).then(() => {
    unbindUserGroup(row);
  });
};
const unbindUserGroup = async (row) => {
  let res = await $unbindUser({ euid: row.euid });
  if (res.code == 0) {
    ElMessage({
      message: t("common.success"),
      type: "success",
    });
    getData();
  } else {
    ElMessage.error(res.msg);
  }
};
const quitUserGroup = async (row) => {
  let res = await $updateUserGroup({ euid: row.euid });
  if (res.code == 0) {
    ElMessage({
      message: t("common.success"),
      type: "success",
    });
    getData();
  } else {
    ElMessage.error(res.msg);
  }
};

const userTypeConfirm = ({ selectedValue, selectedOptions }) => {
  params.value.user_type_id = selectedValue[0];
  mobTypeTxt.value = selectedOptions[0].text;
  console.log(params);
};
const mobClearFilter = () => {
  (params.value.goods_name = ""),
    (params.value.warehouse_code = ""),
    (params.value.from_mode = ""),
    (params.value.cat_name = ""),
    (params.value.current_page = 1);
  params.value.page_size = 10;
  getData();
};
const mobChecked = (v, item) => {
  console.log(item);
  console.log(v);
  if (v == true) {
    currEuids.value.push(item.euid);
  } else {
    currEuids.value.splice(
      currEuids.value.findIndex((o) => o == item.euid),
      1
    );
  }
};
const handleMore=(item)=>{
  item.more=!item.more;
  if(item.more===true){
    item.moreTxt="收起"
    item.moreIcon=ArrowUp
  }else{
    item.moreTxt="全部信息"
    item.moreIcon=ArrowDown
  }
}
watch(langkey, (newVal, oldVal) => {
  console.log("新值:", newVal, "老值:", oldVal);
  getFilter();
});
</script>

<style lang="scss" scoped>
.mob-scroll {
  height: calc(100vh - 300px);
}
</style>
